<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>imageviewer.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-widgets-imageviewer-example.html">Image Viewer Example</a></td><td >imageviewer.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">imageviewer.cpp 示例文件</h1>
<span class="subtitle">widgets/imageviewer/imageviewer.cpp</span>
<!-- $$$widgets/imageviewer/imageviewer.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>
  <span class="preprocessor">#if defined(QT_PRINTSUPPORT_LIB)</span>
  <span class="preprocessor">#include &lt;QtPrintSupport/qtprintsupportglobal.h&gt;</span>
  <span class="preprocessor">#if QT_CONFIG(printdialog)</span>
  <span class="preprocessor">#include &lt;QPrintDialog&gt;</span>
  <span class="preprocessor">#endif</span>
  <span class="preprocessor">#endif</span>

  <span class="preprocessor">#include &quot;imageviewer.h&quot;</span>

  ImageViewer<span class="operator">::</span>ImageViewer()
     : imageLabel(<span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>)
     <span class="operator">,</span> scrollArea(<span class="keyword">new</span> <span class="type"><a href="qscrollarea.html">QScrollArea</a></span>)
     <span class="operator">,</span> scaleFactor(<span class="number">1</span>)
  {
      imageLabel<span class="operator">-</span><span class="operator">&gt;</span>setBackgroundRole(<span class="type"><a href="../qtgui/qpalette.html">QPalette</a></span><span class="operator">::</span>Base);
      imageLabel<span class="operator">-</span><span class="operator">&gt;</span>setSizePolicy(<span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Ignored<span class="operator">,</span> <span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Ignored);
      imageLabel<span class="operator">-</span><span class="operator">&gt;</span>setScaledContents(<span class="keyword">true</span>);

      scrollArea<span class="operator">-</span><span class="operator">&gt;</span>setBackgroundRole(<span class="type"><a href="../qtgui/qpalette.html">QPalette</a></span><span class="operator">::</span>Dark);
      scrollArea<span class="operator">-</span><span class="operator">&gt;</span>setWidget(imageLabel);
      scrollArea<span class="operator">-</span><span class="operator">&gt;</span>setVisible(<span class="keyword">false</span>);
      setCentralWidget(scrollArea);

      createActions();

      resize(<span class="type"><a href="../qtgui/qguiapplication.html">QGuiApplication</a></span><span class="operator">::</span>primaryScreen()<span class="operator">-</span><span class="operator">&gt;</span>availableSize() <span class="operator">*</span> <span class="number">3</span> <span class="operator">/</span> <span class="number">5</span>);
  }


  bool ImageViewer<span class="operator">::</span>loadFile(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>fileName)
  {
      <span class="type"><a href="../qtgui/qimagereader.html">QImageReader</a></span> reader(fileName);
      reader<span class="operator">.</span>setAutoTransform(<span class="keyword">true</span>);
      <span class="keyword">const</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> newImage <span class="operator">=</span> reader<span class="operator">.</span>read();
      <span class="keyword">if</span> (newImage<span class="operator">.</span>isNull()) {
          <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> <span class="type"><a href="../qtgui/qguiapplication.html">QGuiApplication</a></span><span class="operator">::</span>applicationDisplayName()<span class="operator">,</span>
                                   tr(<span class="string">&quot;Cannot load %1: %2&quot;</span>)
                                   <span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(fileName)<span class="operator">,</span> reader<span class="operator">.</span>errorString()));
          <span class="keyword">return</span> <span class="keyword">false</span>;
      }

      setImage(newImage);

      setWindowFilePath(fileName);

      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> message <span class="operator">=</span> tr(<span class="string">&quot;Opened \&quot;%1\&quot;, %2x%3, Depth: %4&quot;</span>)
          <span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(fileName))<span class="operator">.</span>arg(image<span class="operator">.</span>width())<span class="operator">.</span>arg(image<span class="operator">.</span>height())<span class="operator">.</span>arg(image<span class="operator">.</span>depth());
      statusBar()<span class="operator">-</span><span class="operator">&gt;</span>showMessage(message);
      <span class="keyword">return</span> <span class="keyword">true</span>;
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>setImage(<span class="keyword">const</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> <span class="operator">&amp;</span>newImage)
  {
      image <span class="operator">=</span> newImage;
      imageLabel<span class="operator">-</span><span class="operator">&gt;</span>setPixmap(<span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span><span class="operator">::</span>fromImage(image));
      scaleFactor <span class="operator">=</span> <span class="number">1.0</span>;

      scrollArea<span class="operator">-</span><span class="operator">&gt;</span>setVisible(<span class="keyword">true</span>);
      printAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
      fitToWindowAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
      updateActions();

      <span class="keyword">if</span> (<span class="operator">!</span>fitToWindowAct<span class="operator">-</span><span class="operator">&gt;</span>isChecked())
          imageLabel<span class="operator">-</span><span class="operator">&gt;</span>adjustSize();
  }


  bool ImageViewer<span class="operator">::</span>saveFile(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>fileName)
  {
      <span class="type"><a href="../qtgui/qimagewriter.html">QImageWriter</a></span> writer(fileName);

      <span class="keyword">if</span> (<span class="operator">!</span>writer<span class="operator">.</span>write(image)) {
          <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> <span class="type"><a href="../qtgui/qguiapplication.html">QGuiApplication</a></span><span class="operator">::</span>applicationDisplayName()<span class="operator">,</span>
                                   tr(<span class="string">&quot;Cannot write %1: %2&quot;</span>)
                                   <span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(fileName))<span class="operator">,</span> writer<span class="operator">.</span>errorString());
          <span class="keyword">return</span> <span class="keyword">false</span>;
      }
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> message <span class="operator">=</span> tr(<span class="string">&quot;Wrote \&quot;%1\&quot;&quot;</span>)<span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(fileName));
      statusBar()<span class="operator">-</span><span class="operator">&gt;</span>showMessage(message);
      <span class="keyword">return</span> <span class="keyword">true</span>;
  }


  <span class="keyword">static</span> <span class="type">void</span> initializeImageFileDialog(<span class="type"><a href="qfiledialog.html">QFileDialog</a></span> <span class="operator">&amp;</span>dialog<span class="operator">,</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>AcceptMode acceptMode)
  {
      <span class="keyword">static</span> bool firstDialog <span class="operator">=</span> <span class="keyword">true</span>;

      <span class="keyword">if</span> (firstDialog) {
          firstDialog <span class="operator">=</span> <span class="keyword">false</span>;
          <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> picturesLocations <span class="operator">=</span> <span class="type"><a href="../qtcore/qstandardpaths.html">QStandardPaths</a></span><span class="operator">::</span>standardLocations(<span class="type"><a href="../qtcore/qstandardpaths.html">QStandardPaths</a></span><span class="operator">::</span>PicturesLocation);
          dialog<span class="operator">.</span>setDirectory(picturesLocations<span class="operator">.</span>isEmpty() <span class="operator">?</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>currentPath() : picturesLocations<span class="operator">.</span>last());
      }

      <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> mimeTypeFilters;
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qbytearraylist.html">QByteArrayList</a></span> supportedMimeTypes <span class="operator">=</span> acceptMode <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>AcceptOpen
          <span class="operator">?</span> <span class="type"><a href="../qtgui/qimagereader.html">QImageReader</a></span><span class="operator">::</span>supportedMimeTypes() : <span class="type"><a href="../qtgui/qimagewriter.html">QImageWriter</a></span><span class="operator">::</span>supportedMimeTypes();
      foreach (<span class="keyword">const</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> <span class="operator">&amp;</span>mimeTypeName<span class="operator">,</span> supportedMimeTypes)
          mimeTypeFilters<span class="operator">.</span>append(mimeTypeName);
      mimeTypeFilters<span class="operator">.</span>sort();
      dialog<span class="operator">.</span>setMimeTypeFilters(mimeTypeFilters);
      dialog<span class="operator">.</span>selectMimeTypeFilter(<span class="string">&quot;image/jpeg&quot;</span>);
      <span class="keyword">if</span> (acceptMode <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>AcceptSave)
          dialog<span class="operator">.</span>setDefaultSuffix(<span class="string">&quot;jpg&quot;</span>);
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>open()
  {
      <span class="type"><a href="qfiledialog.html">QFileDialog</a></span> dialog(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Open File&quot;</span>));
      initializeImageFileDialog(dialog<span class="operator">,</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>AcceptOpen);

      <span class="keyword">while</span> (dialog<span class="operator">.</span>exec() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">::</span>Accepted <span class="operator">&amp;</span><span class="operator">&amp;</span> <span class="operator">!</span>loadFile(dialog<span class="operator">.</span>selectedFiles()<span class="operator">.</span>first())) {}
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>saveAs()
  {
      <span class="type"><a href="qfiledialog.html">QFileDialog</a></span> dialog(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Save File As&quot;</span>));
      initializeImageFileDialog(dialog<span class="operator">,</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>AcceptSave);

      <span class="keyword">while</span> (dialog<span class="operator">.</span>exec() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">::</span>Accepted <span class="operator">&amp;</span><span class="operator">&amp;</span> <span class="operator">!</span>saveFile(dialog<span class="operator">.</span>selectedFiles()<span class="operator">.</span>first())) {}
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>print()
  {
      Q_ASSERT(imageLabel<span class="operator">-</span><span class="operator">&gt;</span>pixmap());
  <span class="preprocessor">#if QT_CONFIG(printdialog)</span>
      <span class="type">QPrintDialog</span> dialog(<span class="operator">&amp;</span>printer<span class="operator">,</span> <span class="keyword">this</span>);
      <span class="keyword">if</span> (dialog<span class="operator">.</span>exec()) {
          <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="operator">&amp;</span>printer);
          <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> rect <span class="operator">=</span> painter<span class="operator">.</span>viewport();
          <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> size <span class="operator">=</span> imageLabel<span class="operator">-</span><span class="operator">&gt;</span>pixmap()<span class="operator">-</span><span class="operator">&gt;</span>size();
          size<span class="operator">.</span>scale(rect<span class="operator">.</span>size()<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>KeepAspectRatio);
          painter<span class="operator">.</span>setViewport(rect<span class="operator">.</span>x()<span class="operator">,</span> rect<span class="operator">.</span>y()<span class="operator">,</span> size<span class="operator">.</span>width()<span class="operator">,</span> size<span class="operator">.</span>height());
          painter<span class="operator">.</span>setWindow(imageLabel<span class="operator">-</span><span class="operator">&gt;</span>pixmap()<span class="operator">-</span><span class="operator">&gt;</span>rect());
          painter<span class="operator">.</span>drawPixmap(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="operator">*</span>imageLabel<span class="operator">-</span><span class="operator">&gt;</span>pixmap());
      }
  <span class="preprocessor">#endif</span>
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>copy()
  {
  <span class="preprocessor">#ifndef QT_NO_CLIPBOARD</span>
      <span class="type"><a href="../qtgui/qguiapplication.html">QGuiApplication</a></span><span class="operator">::</span>clipboard()<span class="operator">-</span><span class="operator">&gt;</span>setImage(image);
  <span class="preprocessor">#endif // !QT_NO_CLIPBOARD</span>
  }

  <span class="preprocessor">#ifndef QT_NO_CLIPBOARD</span>
  <span class="keyword">static</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> clipboardImage()
  {
      <span class="keyword">if</span> (<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmimedata.html">QMimeData</a></span> <span class="operator">*</span>mimeData <span class="operator">=</span> <span class="type"><a href="../qtgui/qguiapplication.html">QGuiApplication</a></span><span class="operator">::</span>clipboard()<span class="operator">-</span><span class="operator">&gt;</span>mimeData()) {
          <span class="keyword">if</span> (mimeData<span class="operator">-</span><span class="operator">&gt;</span>hasImage()) {
              <span class="keyword">const</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> image <span class="operator">=</span> qvariant_cast<span class="operator">&lt;</span><span class="type"><a href="../qtgui/qimage.html">QImage</a></span><span class="operator">&gt;</span>(mimeData<span class="operator">-</span><span class="operator">&gt;</span>imageData());
              <span class="keyword">if</span> (<span class="operator">!</span>image<span class="operator">.</span>isNull())
                  <span class="keyword">return</span> image;
          }
      }
      <span class="keyword">return</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span>();
  }
  <span class="preprocessor">#endif // !QT_NO_CLIPBOARD</span>

  <span class="type">void</span> ImageViewer<span class="operator">::</span>paste()
  {
  <span class="preprocessor">#ifndef QT_NO_CLIPBOARD</span>
      <span class="keyword">const</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> newImage <span class="operator">=</span> clipboardImage();
      <span class="keyword">if</span> (newImage<span class="operator">.</span>isNull()) {
          statusBar()<span class="operator">-</span><span class="operator">&gt;</span>showMessage(tr(<span class="string">&quot;No image in clipboard&quot;</span>));
      } <span class="keyword">else</span> {
          setImage(newImage);
          setWindowFilePath(<span class="type"><a href="../qtcore/qstring.html">QString</a></span>());
          <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> message <span class="operator">=</span> tr(<span class="string">&quot;Obtained image from clipboard, %1x%2, Depth: %3&quot;</span>)
              <span class="operator">.</span>arg(newImage<span class="operator">.</span>width())<span class="operator">.</span>arg(newImage<span class="operator">.</span>height())<span class="operator">.</span>arg(newImage<span class="operator">.</span>depth());
          statusBar()<span class="operator">-</span><span class="operator">&gt;</span>showMessage(message);
      }
  <span class="preprocessor">#endif // !QT_NO_CLIPBOARD</span>
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>zoomIn()
  {
      scaleImage(<span class="number">1.25</span>);
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>zoomOut()
  {
      scaleImage(<span class="number">0.8</span>);
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>normalSize()
  {
      imageLabel<span class="operator">-</span><span class="operator">&gt;</span>adjustSize();
      scaleFactor <span class="operator">=</span> <span class="number">1.0</span>;
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>fitToWindow()
  {
      bool fitToWindow <span class="operator">=</span> fitToWindowAct<span class="operator">-</span><span class="operator">&gt;</span>isChecked();
      scrollArea<span class="operator">-</span><span class="operator">&gt;</span>setWidgetResizable(fitToWindow);
      <span class="keyword">if</span> (<span class="operator">!</span>fitToWindow)
          normalSize();
      updateActions();
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>about()
  {
      <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>about(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;About Image Viewer&quot;</span>)<span class="operator">,</span>
              tr(<span class="string">&quot;&lt;p&gt;The &lt;b&gt;Image Viewer&lt;/b&gt; example shows how to combine QLabel &quot;</span>
                 <span class="string">&quot;and QScrollArea to display an image. QLabel is typically used &quot;</span>
                 <span class="string">&quot;for displaying a text, but it can also display an image. &quot;</span>
                 <span class="string">&quot;QScrollArea provides a scrolling view around another widget. &quot;</span>
                 <span class="string">&quot;If the child widget exceeds the size of the frame, QScrollArea &quot;</span>
                 <span class="string">&quot;automatically provides scroll bars. &lt;/p&gt;&lt;p&gt;The example &quot;</span>
                 <span class="string">&quot;demonstrates how QLabel's ability to scale its contents &quot;</span>
                 <span class="string">&quot;(QLabel::scaledContents), and QScrollArea's ability to &quot;</span>
                 <span class="string">&quot;automatically resize its contents &quot;</span>
                 <span class="string">&quot;(QScrollArea::widgetResizable), can be used to implement &quot;</span>
                 <span class="string">&quot;zooming and scaling features. &lt;/p&gt;&lt;p&gt;In addition the example &quot;</span>
                 <span class="string">&quot;shows how to use QPainter to print an image.&lt;/p&gt;&quot;</span>));
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>createActions()
  {
      <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>fileMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;File&quot;</span>));

      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>openAct <span class="operator">=</span> fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;Open...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>ImageViewer<span class="operator">::</span>open);
      openAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Open);

      saveAsAct <span class="operator">=</span> fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;Save As...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>ImageViewer<span class="operator">::</span>saveAs);
      saveAsAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);

      printAct <span class="operator">=</span> fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;Print...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>ImageViewer<span class="operator">::</span>print);
      printAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Print);
      printAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);

      fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();

      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>exitAct <span class="operator">=</span> fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;E&amp;xit&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">::</span>close);
      exitAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+Q&quot;</span>));

      <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>editMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;Edit&quot;</span>));

      copyAct <span class="operator">=</span> editMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;Copy&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>ImageViewer<span class="operator">::</span>copy);
      copyAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Copy);
      copyAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);

      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>pasteAct <span class="operator">=</span> editMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;Paste&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>ImageViewer<span class="operator">::</span>paste);
      pasteAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Paste);

      <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>viewMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;View&quot;</span>));

      zoomInAct <span class="operator">=</span> viewMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Zoom &amp;In (25%)&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>ImageViewer<span class="operator">::</span>zoomIn);
      zoomInAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>ZoomIn);
      zoomInAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);

      zoomOutAct <span class="operator">=</span> viewMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Zoom &amp;Out (25%)&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>ImageViewer<span class="operator">::</span>zoomOut);
      zoomOutAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>ZoomOut);
      zoomOutAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);

      normalSizeAct <span class="operator">=</span> viewMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;Normal Size&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>ImageViewer<span class="operator">::</span>normalSize);
      normalSizeAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+S&quot;</span>));
      normalSizeAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);

      viewMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();

      fitToWindowAct <span class="operator">=</span> viewMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;Fit to Window&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>ImageViewer<span class="operator">::</span>fitToWindow);
      fitToWindowAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
      fitToWindowAct<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      fitToWindowAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+F&quot;</span>));

      <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>helpMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;Help&quot;</span>));

      helpMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;About&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>ImageViewer<span class="operator">::</span>about);
      helpMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;About &amp;Qt&quot;</span>)<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>aboutQt);
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>updateActions()
  {
      saveAsAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="operator">!</span>image<span class="operator">.</span>isNull());
      copyAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="operator">!</span>image<span class="operator">.</span>isNull());
      zoomInAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="operator">!</span>fitToWindowAct<span class="operator">-</span><span class="operator">&gt;</span>isChecked());
      zoomOutAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="operator">!</span>fitToWindowAct<span class="operator">-</span><span class="operator">&gt;</span>isChecked());
      normalSizeAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="operator">!</span>fitToWindowAct<span class="operator">-</span><span class="operator">&gt;</span>isChecked());
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>scaleImage(<span class="type">double</span> factor)
  {
      Q_ASSERT(imageLabel<span class="operator">-</span><span class="operator">&gt;</span>pixmap());
      scaleFactor <span class="operator">*</span><span class="operator">=</span> factor;
      imageLabel<span class="operator">-</span><span class="operator">&gt;</span>resize(scaleFactor <span class="operator">*</span> imageLabel<span class="operator">-</span><span class="operator">&gt;</span>pixmap()<span class="operator">-</span><span class="operator">&gt;</span>size());

      adjustScrollBar(scrollArea<span class="operator">-</span><span class="operator">&gt;</span>horizontalScrollBar()<span class="operator">,</span> factor);
      adjustScrollBar(scrollArea<span class="operator">-</span><span class="operator">&gt;</span>verticalScrollBar()<span class="operator">,</span> factor);

      zoomInAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(scaleFactor <span class="operator">&lt;</span> <span class="number">3.0</span>);
      zoomOutAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(scaleFactor <span class="operator">&gt;</span> <span class="number">0.333</span>);
  }

  <span class="type">void</span> ImageViewer<span class="operator">::</span>adjustScrollBar(<span class="type"><a href="qscrollbar.html">QScrollBar</a></span> <span class="operator">*</span>scrollBar<span class="operator">,</span> <span class="type">double</span> factor)
  {
      scrollBar<span class="operator">-</span><span class="operator">&gt;</span>setValue(<span class="type">int</span>(factor <span class="operator">*</span> scrollBar<span class="operator">-</span><span class="operator">&gt;</span>value()
                              <span class="operator">+</span> ((factor <span class="operator">-</span> <span class="number">1</span>) <span class="operator">*</span> scrollBar<span class="operator">-</span><span class="operator">&gt;</span>pageStep()<span class="operator">/</span><span class="number">2</span>)));
  }

</pre>
</div>
<!-- @@@widgets/imageviewer/imageviewer.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
